// $Id: user_news.js 290 2008-10-06 14:13:14Z hugo.wetterberg $

var UserNews = function(block) {
  var keyed_feeds = function(feed_data) {
      var feeds = {},
          feed_count = feed_data.length;
      for (var path in feed_data) {
        feeds[path] = feed_data[path][1];
      }
      return feeds;
    };
  
  var paginatorSettingsForced = {
      pageSize: 4,
      feeds: keyed_feeds(Drupal.user_channels.subscriptions(2,true)),
      service: 'user_channels.feed_news_items', 
      filters: {'promoted':true},
      fields: ['nid', 'title', 'type', 'comment_count', 'created', 'feedapi_external_url', 'comment']
    };
    
  var paginatorSettingsNormal = {
    pageSize: 15,
    feeds: keyed_feeds(Drupal.user_channels.subscriptions(2)),
    service: 'user_channels.feed_news_items', 
    filters: {'promoted':false, 'category_filters': true},
    fields: ['nid', 'title', 'type', 'comment_count', 'created', 'feedapi_external_url', 'comment']
  };
  
  jQuery(block).find('.promoted-news-wrapper').go_paginator( paginatorSettingsForced);
  jQuery(block).find('.normal-news-wrapper').go_paginator(paginatorSettingsNormal);
    
    jQuery(block).find('.promoted-news-wrapper').add('.normal-news-wrapper').bind('construct_items', function(evt, data)
      {
      var items = data.items, 
        list = $(this).find('.listing.animation');
      
      for(var el, item, i = 0; i < items.length; i++)
      {
        item = items[i];
        el = list.find('li:eq(' + i + ')');
        if(item[6] != 0 && !el.find('.comment-link').length)
        {
          el.append('<a class="comment-link"><em></em></a>');
        }
        if(!el.find('.node-type').length)
        {
          el.find('.permalink').before('<span class="node-type-wrapper"><img class="node-type" width="20" height="20" /></span>');
        }
        el.find('.node-type').attr('src', Drupal.settings.user_map.favicon_path +'/'+ item[7] +'/icon/dark.png');
        if (item[6] != 0) {
          el.find('.comment-link').attr('href', Drupal.settings.basePath + 'js-node-link/'+ item[0] +'#comments').find('em').text(item[3]);
        }
        if (item[5] !== null) {
          el.find('.permalink').attr('href', item[5]).attr('target', '_blank').unbind('click');
        }
      }
    });
};

jQuery(document).ready(function(){
  var block = jQuery('.block-user_channels').get(0);
  if (block) {
    new UserNews(block);
  }
});
